import sys

from PyQt5.QtCore import pyqtSignal, Qt, pyqtProperty
from PyQt5.QtGui import QColor, QFont
from PyQt5.QtWidgets import QFrame, QHBoxLayout, QLabel
from qfluentwidgets import LineEdit, FluentIcon, ColorPickerButton, themeColor
from components import OpenFileButton
from .label import Text15Label


class LineEditFrame(QFrame):
    open_file = pyqtSignal()

    def __init__(self, parent=None):
        super().__init__(parent)
        self.hBox = QHBoxLayout(self)
        self.hBox.setContentsMargins(0, 0, 0, 0)
        self.hBox.setSpacing(10)

        self.bodyLabel = Text15Label()
        self.bodyLabel.setText('选择文件:')
        self.bodyLabel.setFixedWidth(100)
        self.lineEdit = LineEdit()
        self.lineEdit.setClearButtonEnabled(True)
        self.openFileButton = OpenFileButton()
        self.openFileButton.setIcon(FluentIcon.FOLDER)
        self.openFileButton.setFixedWidth(150)
        self.openFileButton.setText('选择文件夹')

        self.hBox.addWidget(self.bodyLabel)
        self.hBox.addWidget(self.lineEdit)
        self.hBox.addWidget(self.openFileButton)

        self.openFileButton.clicked.connect(self.openFileEvent)

    def setLabelText(self, text):
        self.bodyLabel.setText(text)
        self.lineEdit.setPlaceholderText(text[:-1] + '...')
        self.update()

    def setButtonText(self, text):
        self.openFileButton.setText(text)
        self.update()

    def setButtonIcon(self, icon):
        self.openFileButton.setIcon(icon)
        self.update()

    def openFileEvent(self):
        file = self.openFileButton.getExistingDirectory()
        self.lineEdit.setText(file)

    def lineEditText(self):
        return self.lineEdit.text()

    def clear(self):
        self.lineEdit.clear()


class ColorFrame(QFrame):
    def __init__(self, parent=None):
        super().__init__(parent)
        self._font = QFont('微软雅黑', 10)

        self.hBox = QHBoxLayout(self)
        self.hBox.setContentsMargins(0, 0, 0, 0)
        # self.hBox.addSpacing(10)
        self.hBox.setAlignment(Qt.AlignmentFlag.AlignLeft)

        self.label = Text15Label(self)
        self.label.setAlignment(Qt.AlignmentFlag.AlignLeft | Qt.AlignmentFlag.AlignVCenter)
        self.label.setFixedWidth(100)
        self.colorButton = ColorPickerButton(themeColor(), '字体颜色', self, True)
        self.colorButton.setFixedWidth(100)

        self.hBox.addWidget(self.label, 0, Qt.AlignmentFlag.AlignLeft)
        self.hBox.addWidget(self.colorButton, 0, Qt.AlignmentFlag.AlignLeft)

        self.setFont(self._font)

    def setText(self, text: str):
        self.label.setText(text)
        self.update()

    def text(self) -> str:
        return self.label.text()

    def setFont(self, font: QFont):
        self.label.setFont(font)

    def font(self):
        return self.label.font()

    def color(self):
        return self.colorButton.color


if __name__ == '__main__':
    from PyQt5.QtWidgets import QApplication

    app = QApplication(sys.argv)
    win = ColorFrame()
    win.setText('Hello')
    win.resize(1000, 700)
    win.show()
    sys.exit(app.exec())
